﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;

namespace WX.WMS.DataModel
{
    public class BaseDAO : WMSDataContext
    {

        public string sequenceName = "";
        public BaseDAO()
            : base(System.Configuration.ConfigurationSettings.AppSettings["MainConStr"].ToString())
        {

        }

        public BaseDAO(System.Data.IDbConnection connection)
            : base(connection)
        {

        }

        public void BeginTransaction()
        {
            if (this.Connection.State == ConnectionState.Closed)
                this.Connection.Open();
            DbTransaction tran = this.Connection.BeginTransaction();
            this.Transaction = tran;
        }

        public void RollBack()
        {
            try
            {
                this.Transaction.Rollback();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (this.Connection.State != ConnectionState.Closed)
                    this.Connection.Close();
            }
        }

        public void Commit()
        {
            try
            {
                this.Transaction.Commit();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (this.Connection.State != ConnectionState.Closed)
                    this.Connection.Close();
            }
        }

        public Int32 GetNextId()
        {
            return GetNextId(sequenceName);
        }

        public Int32 GetNextId(string sequenceName)
        {
            decimal nextId = this.GetSequence(sequenceName);
            return Convert.ToInt32(nextId);
        }

        public DateTime GetDatabaseTime()
        {

            DateTime? result = new DateTime?();

            this.GetDatabaseTime(ref result);


            return System.Convert.ToDateTime(result.ToString());
        }

        public string GetConnectString()
        {
            return System.Configuration.ConfigurationSettings.AppSettings["MainConStr"].ToString();
        }

        public DataTable ExecuteQuery(string strSql)
        {
            SqlDataAdapter adapter = new SqlDataAdapter(strSql, GetConnectString());
            DataTable dtResult = new DataTable();
            adapter.Fill(dtResult);
            adapter.Dispose();

            return dtResult;
        }

    }
}
